package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.ResponseResult;
import com.woniuxy.entity.AdminUser;
import com.woniuxy.jwt.Audience;
import com.woniuxy.jwt.JwtUtil;
import com.woniuxy.service.AdminUserService;
import com.woniuxy.service.ShopUserService;
import com.woniuxy.service.UseUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping("adminUser")
@Slf4j
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private Audience audience;

    @Autowired
    //用户的登录
    private UseUserService useUserService;

    @Autowired
    private ShopUserService shopUserService;

    /**
     * 管理员登录
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResponseResult<Void> login(String account, String password, HttpServletResponse response){
        //根据账号和密码查询数据库
        UsernamePasswordToken token = new UsernamePasswordToken(account,password);
        Subject subject = SecurityUtils.getSubject();

        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            return new ResponseResult<Void>(500,"账号不存在",null);
        }catch (IncorrectCredentialsException e){
            return new ResponseResult<Void>(500,"密码错误",null);
        }

        AdminUser adminUser = (AdminUser)subject.getPrincipal();
        //产生jwt
        String jwt = JwtUtil.createJWT(adminUser.getId(),account,0,audience);
        response.setHeader("Authorization",jwt);
        return ResponseResult.SUCCESS;

    }

    /**
     * 用户的登录
     * @param account
     * @param pwd
     * @return
     */
    @PostMapping("userlogin")
    public ResponseResult<Integer> userLogin(String account,String pwd,HttpServletResponse response){
        ResponseResult<Integer> responseResult = useUserService.userLogin(account, pwd);
        if (responseResult.getCode()==200) {
            //产生jwt
            String jwt = JwtUtil.createJWT(responseResult.getData(), account,1, audience);
            log.info(jwt);
            response.setHeader("Authorization", jwt);
        }
        return responseResult;
    }

    /**
     * 商家的登录
     * @param account
     * @param pwd
     * @return
     */
    @PostMapping("shoplogin")
    public ResponseResult<Integer> shopLogin(String account,String pwd,HttpServletResponse response){
        ResponseResult<Integer> responseResult = shopUserService.shopLogin(account, pwd);
        if (responseResult.getCode()==200) {
            //产生jwt
            String jwt = JwtUtil.createJWT(responseResult.getData(), account,2, audience);
            response.setHeader("Authorization", jwt);
        }
        return responseResult;
    }

    /**
     * 管理员注册
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/regist")
    public ResponseResult<String> add(String account,String password){
        //首先查询账号是否注册过
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        AdminUser isAdminUser = adminUserService.getOne(queryWrapper);
        if (isAdminUser!=null){
            return new ResponseResult<String>(500,"账号已被注册","注册失败");
        }


        //新建用户的实体类
        AdminUser adminUser = new AdminUser().setAccount(account).setPwd(password);
        //调用service给数据库增加一个字段
        boolean save = adminUserService.save(adminUser);
        if(save){
            return new ResponseResult<String>(200,"注册成功","注册成功");
        }else{
            return new ResponseResult<String>(500,"注册失败","注册失败");
        }
    }

    @PostMapping("getmenu")
    public ResponseResult<Void> getMenu(@RequestHeader(value = "jwt",required = false)String jwt){
        log.info("jwt是"+jwt);
        if (JwtUtil.parseJwt(jwt,audience.getBase64Secret())){
            int id = JwtUtil.getUserId(jwt,audience.getBase64Secret());
            String account = JwtUtil.getAccount(jwt,audience.getBase64Secret());
            log.info(account+id);
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.ERR;
    }

    @PostMapping("teststring")
    public ResponseResult<String> testString(String str){
        ResponseResult<String> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setData(str);
        responseResult.setMsg("go");
        return responseResult;
    }
}
